package com.bootdo.blog.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.WmsConfig;
import com.bootdo.common.poi.ExcelUtil;
import com.sun.deploy.net.HttpResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.blog.domain.WineDO;
import com.bootdo.blog.service.WineService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-03-12 07:50:17
 */
@Controller
@RequestMapping("/blog/wine")
public class WineController {
    @Autowired
    private WineService wineService;

    @GetMapping()
    @RequiresPermissions("blog:wine:wine")
    String Wine() {
        return "blog/wine/wine";
    }

    @GetMapping("/statistical")
    @RequiresPermissions("blog:wine:statistical")
    String statistical() {
        return "blog/wine/statistical";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("blog:wine:list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<WineDO> wineList = wineService.list(query);
        System.out.println(wineList);
        int total = wineService.count(query);
        PageUtils pageUtils = new PageUtils(wineList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("blog:wine:save")
    String add() {
        return "blog/wine/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("blog:wine:update")
    String edit(@PathVariable("id") Integer wineId, Model model) {
        System.out.println("id:" + wineId);
        WineDO wineDO = wineService.get(wineId);
        System.out.println(wineDO);
        model.addAttribute("wineDO", wineDO);
        return "blog/wine/edit";
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{wineId}")
    @RequiresPermissions("blog:wine:info")
    public R info(@PathVariable("wineId") Integer wineId) {
        WineDO wine = wineService.get(wineId);
        return R.ok().put("wine", wine);
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("blog:wine:save")
    public R save(WineDO wine) {
        if (wineService.save(wine) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("blog:wine:update")
    public R update(WineDO wine) {
        wineService.update(wine);
        return R.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("blog:wine:remove")
    public R remove(@RequestParam("id") Integer wineId) {
        if (wineService.remove(wineId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("blog:wine:remove")
    public R remove(@RequestParam("ids[]") Integer[] wineIds) {
        wineService.batchRemove(wineIds);

        return R.ok();
    }

    @RequiresPermissions("blog:wine:export")
    @ResponseBody
    @GetMapping("/export")
    public R export(WineDO wineDO) {
        try {
            ExcelUtil.export2File(WmsConfig.getDownloadPath(), "葡萄酒信息", "葡萄酒信息表", WineDO.class, wineService.list2(wineDO));
            return R.ok("导出成功");
        } catch (Exception e) {
            return R.error("报表导出异常:" + e);
        }
    }

    /**
    * 功能描述:饼图

     * @param

    * @Author:  liuxiaolong
    * @return:
    * @Date:    2021/03/12 23:46:00
    */
    @RequiresPermissions("blog:wine:statistical")
    @ResponseBody
    @PostMapping("/pieChart")
    public Map<String, Object> pieChart(){
        Map<String, Object> map = new HashMap<String, Object>();
        int no = wineService.count2(0);
        int yes = wineService.count2(1);
        int count = wineService.count(new HashMap<>());
        map.put("合格", yes);
        map.put("不合格", no);
        map.put("未检测", count-yes-no);
        System.out.println(map);
        return map;
    }
}
